from machine import Pin
import utime
import network
import time
import lc
from machine import RTC
from machine import Pin, I2C,ADC
import lcd12864
import sht31
import PFC8563
import machine
import zk
from esp32 import gpio_deep_sleep_hold

sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)  
rtc = RTC()
pot = ADC(Pin(lc.voltagePin))
pot.atten(ADC.ATTN_6DB) 
pin1 = Pin(lc.led1, Pin.OUT)
# leda = Pin(lc.leda, Pin.OUT,pull=Pin.PULL_HOLD)
leda = Pin(lc.leda, Pin.OUT)
ps=Pin(lc.voltageSwitch, Pin.OUT)
dcS=Pin(lc.dcdcSwitch, Pin.OUT)
dcS.on()
i2c = I2C(0,scl=Pin(lc.scl), sda=Pin(lc.sda),freq=400000)
lcd12864.i2c=i2c
sht31.i2c=i2c
PFC8563.i2c=i2c
R_=0
T_=0
U_=0
wt=0
rtc = machine.RTC()
rzk=b''
for i in zk.n32[:10]:
    rzk+=i
print(len(rzk))
for i in zk.ASCII :#640
    rzk+=i
print(len(rzk))
for i in zk.n16 :#1115
    rzk+=i
print(len(rzk))
for i in zk.xj[:6] :#1371
    rzk+=i
print(len(rzk))
rtc.memory(rzk)

pin1.value(lc.ledon)
# key1=9
sp1=Pin(lc.key1,Pin.IN)

def irqsync(sp1):
#     disable_irq()
    print("irq sync time")

#     PFC8563.sync(tp)
#     tpe()
# sp1.irq(trigger=Pin.IRQ_FALLING,handler=irqsync)
def showSec(s):
    # showSec(60)
    global R_
    global T_
    global U_
    if(s%5==0):
        ps.on()
        sht31.sht31m(i2c)
        utime.sleep_ms(5)
        U_=[]
        for i in range(20):
            U_.append(pot.read())
        ps.off()
        lcd12864.print8("%.2fV   "%(lc.getvolt(U_)),80,4)
        machine.lightsleep(15)
        (T_,R_)=sht31.sht31r(i2c)
        lcd12864.st1(T_,R_)
    lcd12864.print8(f"{s:02d}",80,2)
#     print(T_,R_)

def tp(str):
    print(str)
    lcd12864.print8(str+"     ",0,5)

def tpe():
        utime.sleep_ms(100)
        da=i2c.readfrom_mem(81,2,8)
        m=((da[1]>>4) * 10) + (da[1]&0x0f)
        h=((da[2]>>4) * 10) + (da[2]&0x0f)
        d=((da[3]>>4) * 10) + (da[3]&0x0f)
        mo=((da[5]>>4) * 10) + (da[5]&0x0f)
        tp(f"{mo:02d}-{d:02d} {h:02d}:{m:02d}")

    

def f():
    global U_
    global wt
    wt=0
    print(i2c.scan()) 
    lcd12864.lcdInit()
    lcd12864.clearScreen()
    ps=Pin(lc.voltageSwitch,Pin.OUT)
    pot = ADC(Pin(lc.voltagePin))
    pot.atten(ADC.ATTN_6DB) 
#     leda.value(lc.ledon)
    s=showtimehm()
    if(s>60):
        pin1.value(lc.ledon)
        PFC8563.sync(tp)
        tpe()
        pin1.value(lc.ledoff)
    ps.on()
    utime.sleep_ms(5)
    U_=[]
    for i in range(20):
        U_.append(pot.read())
    ps.off()
    for i in range(15):
        if(sp1.value()==1):
            PFC8563.sync(tp)
            tpe()
        t=rtc.datetime()
        s=showtimehm()
        showSec(s)
        utime.sleep_us(1000000-t[7])

    pin1.value(lc.ledoff)
    
    while 1 :
        # showtimehm()

        t=rtc.datetime()
#         print(t)
#         print(int((1000000-t[7])/1000))
        # utime.sleep_us(1000000-t[7])
        # machine.lightsleep(int((10000000-t[7])/1000)) 
        machine.lightsleep(int((1000000-t[7])/1000)) 
        # machine.deepsleep((int((60000000-t[7])/1000)))
        s+=1
        if(s>=60):
            s=showtimehm()
        showSec(s)
        wt+=1
#         print(3600-wt)
        if(wt>30):
            lcd12864.print8(f"   ",80,2)
            da=i2c.readfrom_mem(81,2,1)
            s=((da[0]>>4) * 10) + (da[0]&0x0f)
            dcS.off()
            leda.value(lc.ledoff)
            machine.deepsleep((60-s)*1000)
            wt=0
        
def deepwake():
    while True:
        t=rtc.datetime()
        print(time.mktime(t))
        print("{0:0>2d}:{1:0>2d}:{2:0>2d}\t\t{3:0>3d}".format(t[4],t[5],t[6]))
        t=rtc.datetime()
        utime.sleep_us(1000000-t[7])
        print(int((1000000-t[7]))/1000)
        machine.deepsleep(10000)
def showtimehm():
    ps.on()
    global U_
    (y,mo,d,w,h,m,s)=PFC8563.getdatatime()
    utime.sleep_ms(5)
    U_=[]
    for i in range(20):
        U_.append(pot.read())
    print(U_)
    ps.off()
    lcd12864.st(h,m,y,mo,d,w,lc.getvolt(U_))
    return s
if __name__ == "__main__":
#     print(i2c.scan()) 
    f()